package com.hbk.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.hbk.base.util.StringUtil;
import com.hbk.service.IOssService;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

@Service
public class AliOssServiceImpl implements IOssService {

    private static String END_POINT = "https://oss-cn-hangzhou.aliyuncs.com";
    private static String ACCESS_KEY = "LTAI5tPLHZwhQnZobPYkGi7j";
    private static String ACCESS_SECRET = "931Grt6tdqoRkoBiJvF5Oe9Ow2MrcY";
    private static String BUCKET_NAME = "hbk-view";
    private static String FILE_PREFIX = "https://hbk-view.oss-cn-hangzhou.aliyuncs.com/";

    public String put(String key, String fileName){
        OSS client = new OSSClientBuilder().build(END_POINT, ACCESS_KEY, ACCESS_SECRET);
        client.putObject(BUCKET_NAME, key, new File(fileName));
        client.shutdown();
        return FILE_PREFIX + key;
    }

    public String put(String key, File file){
        OSS client = new OSSClientBuilder().build(END_POINT, ACCESS_KEY, ACCESS_SECRET);
        client.putObject(BUCKET_NAME, key, file);
        client.shutdown();
        return FILE_PREFIX + key;
    }

    public String put(String key, InputStream inputStream){
        OSS client = new OSSClientBuilder().build(END_POINT, ACCESS_KEY, ACCESS_SECRET);
        client.putObject(BUCKET_NAME, key, inputStream);
        client.shutdown();
        IOUtils.closeQuietly(inputStream);
        return FILE_PREFIX + key;
    }

    public String putUrl(String key, String src){
        if(StringUtil.isEmpty(src)){
            return "";
        }
        if(src.startsWith(FILE_PREFIX)){
            return src;
        }
        URL url = null;
        InputStream inputStream = null;
        OSS client = null;

        boolean temp = false;
        try{
            url = new URL(src);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true);
            conn.connect();
            inputStream = conn.getInputStream();
            client = new OSSClientBuilder().build(END_POINT, ACCESS_KEY, ACCESS_SECRET);
            client.putObject(BUCKET_NAME, key, inputStream);
            client.shutdown();
            temp = true;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(client!=null){
                client.shutdown();
                IOUtils.closeQuietly(inputStream);
            }
        }
        if(temp){
            return FILE_PREFIX + key;
        }else{
            return null;
        }

    }

    public String getPrefix(){
        return FILE_PREFIX;
    }

    /** 删除oss上的图片 */
    public void delete(String url){
        if(StringUtil.isEmpty(url)){
            return;
        }
        if(!url.startsWith(FILE_PREFIX)){
            return;
        }
        String key = url.replace(FILE_PREFIX, "");
        OSS client = new OSSClientBuilder().build(END_POINT, ACCESS_KEY, ACCESS_SECRET);
        client.deleteObject(BUCKET_NAME, key);
    }

}
